<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="盘库单号" prop="checkGoodsNumber">
        <el-input
          v-model="queryParams.checkGoodsNumber"
          placeholder="请输入盘库单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="盘库任务" prop="checkGoodsTask">
        <el-input
          v-model="queryParams.checkGoodsTask"
          placeholder="请输入盘库任务"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--el-form-item label="重量(吨)" prop="weight">
        <el-input
          v-model="queryParams.weight"
          placeholder="请输入重量(吨)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="件数" prop="numberOfPiece">
        <el-input
          v-model="queryParams.numberOfPiece"
          placeholder="请输入件数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="已盘重量(吨)" prop="checkedWeight">
        <el-input
          v-model="queryParams.checkedWeight"
          placeholder="请输入已盘重量(吨)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="已盘件数" prop="checkedOfPiece">
        <el-input
          v-model="queryParams.checkedOfPiece"
          placeholder="请输入已盘件数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="盘库确认人" prop="checkGoodsConfirmPerson">
        <el-input
          v-model="queryParams.checkGoodsConfirmPerson"
          placeholder="请输入盘库确认人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="盘库确认时间" prop="checkGoodsConfirmTime">
        <el-date-picker clearable
          v-model="queryParams.checkGoodsConfirmTime"
          style="width: 205px"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择盘库确认时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="queryParams.remark"
          placeholder="请输入备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="作业状态" prop="jobStatus">
        <el-select v-model="queryParams.jobStatus" placeholder="请选择作业状态" clearable>
          <el-option
            v-for="dict in dict.type.job_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="园区代码" prop="parkCode">
        <el-input
          v-model="queryParams.parkCode"
          placeholder="请输入园区代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item-->
      <el-form-item label="创建者" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          style="width: 205px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-col>
      <!--el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['inventory:receipt:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['inventory:receipt:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['inventory:receipt:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['inventory:receipt:export']"
        >导出</el-button>
      </el-col-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-s-home"
          size="mini"
          :disabled="single"
          @click="handleInventoryRevoke"
          v-hasPermi="['inventory:receipt:revork']"
        >撤销完成</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" @computeTable="computeTableHeight"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="receiptList" @selection-change="handleSelectionChange" border :height="tableHeight" :row-style="{height:'35px'}" :cell-style="{padding:'0px'}">
      <el-table-column type="selection" width="45" align="center" />
      <el-table-column type="index" prop="" width="55" label="序号" align="center" >
        <template slot-scope="scope">
          <span>{{ scope.$index+1 +(queryParams.pageNum-1)*queryParams.pageSize }}</span>
        </template>
      </el-table-column>
      <!--el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100%">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['inventory:receipt:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['inventory:receipt:remove']"
          >删除</el-button>
        </template>
      </el-table-column-->
      <!--el-table-column label="盘库ID" align="center" prop="checkGoodsId" /-->
      <el-table-column label="盘库单号" align="center" prop="checkGoodsNumber" width="160" >
        <template slot-scope="scope">
          <router-link :to="'/inventory/task-relategoods/index/' + scope.row.checkGoodsId" class="link-type">
            <span>{{ scope.row.checkGoodsNumber }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="盘库任务" align="center" prop="checkGoodsTask" width="240" :show-overflow-tooltip="true" />
      <el-table-column label="重量(吨)" align="center" prop="weight" width="100" />
      <el-table-column label="件数" align="center" prop="numberOfPiece" />
      <el-table-column label="已盘(吨)" align="center" prop="checkedWeight" width="100" />
      <el-table-column label="已盘件数" align="center" prop="checkedOfPiece" />
      <el-table-column label="盘库确认" align="center" prop="checkGoodsConfirmPerson" />
      <el-table-column label="盘库确认时间" align="center" prop="checkGoodsConfirmTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkGoodsConfirmTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" width="180" :show-overflow-tooltip="true" />
      <!--el-table-column label="作业状态" align="center" prop="jobStatus" /-->
      <el-table-column label="创建者" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改盘库单据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="盘库单号" prop="checkGoodsNumber">
          <el-input v-model="form.checkGoodsNumber" placeholder="请输入盘库单号" clearable />
        </el-form-item>
        <el-form-item label="盘库任务" prop="checkGoodsTask">
          <el-input v-model="form.checkGoodsTask" placeholder="请输入盘库任务" clearable />
        </el-form-item>
        <el-form-item label="重量(吨)" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入重量(吨)" clearable />
        </el-form-item>
        <el-form-item label="件数" prop="numberOfPiece">
          <el-input v-model="form.numberOfPiece" placeholder="请输入件数" clearable />
        </el-form-item>
        <el-form-item label="已盘重量(吨)" prop="checkedWeight">
          <el-input v-model="form.checkedWeight" placeholder="请输入已盘重量(吨)" clearable />
        </el-form-item>
        <el-form-item label="已盘件数" prop="checkedOfPiece">
          <el-input v-model="form.checkedOfPiece" placeholder="请输入已盘件数" clearable />
        </el-form-item>
        <el-form-item label="盘库确认人" prop="checkGoodsConfirmPerson">
          <el-input v-model="form.checkGoodsConfirmPerson" placeholder="请输入盘库确认人" clearable />
        </el-form-item>
        <el-form-item label="盘库确认时间" prop="checkGoodsConfirmTime">
          <el-date-picker clearable
            v-model="form.checkGoodsConfirmTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择盘库确认时间"
            style="width: 340px;">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" clearable />
        </el-form-item>
        <el-form-item label="作业状态" prop="jobStatus">
          <el-select v-model="form.jobStatus" placeholder="请选择作业状态" style="width: 340px;" clearable>
            <el-option
              v-for="dict in dict.type.job_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.label"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReceipt, getReceipt, delReceipt, addReceipt, updateReceipt, revokeCheckGoods } from "@/api/inventory/receipt";

export default {
  name: "Receipt",
  dicts: ['job_status'],
  data() {
    return {
      // 定义列表高度
      tableHeight: 430,
      // 定义查询高度
      queryHeight: 200,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 盘库单据表格数据
      receiptList: [],
      //选择列表
      selectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 作业状态时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        checkGoodsNumber: null,
        checkGoodsTask: null,
        weight: null,
        numberOfPiece: null,
        checkedWeight: null,
        checkedOfPiece: null,
        checkGoodsConfirmPerson: null,
        checkGoodsConfirmTime: null,
        remark: null,
        jobStatus: null,
        parkCode: null,
        createBy: null,
        createTime: null,
      },
      // 表单参数
      form: {},
      //作业状态
      jobStatus: '3',
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    this.$nextTick(() =>{
      // 根据浏览器高度设置初始高度 - 按钮行高度28 - 页码行高度28 - （面包屑+TAB行）高度160
      this.queryHeight = this.$refs.queryForm.$el.offsetHeight;
      this.tableHeight = window.innerHeight - this.$refs.queryForm.$el.offsetHeight - 28 - 28 - 160;
      // 监听浏览器高度变化，改变表格高度
      window.onresize = () =>{
        this.tableHeight = window.innerHeight - this.$refs.queryForm.$el.offsetHeight - 28 - 28 - 160;
      }
    })
  },
  methods: {
    /** 查询盘库单据列表 */
    getList() {
      this.queryParams.jobStatus = this.jobStatus;
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listReceipt(this.queryParams).then(response => {
        this.receiptList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        checkGoodsId: null,
        checkGoodsNumber: null,
        checkGoodsTask: null,
        weight: null,
        numberOfPiece: null,
        checkedWeight: null,
        checkedOfPiece: null,
        checkGoodsConfirmPerson: null,
        checkGoodsConfirmTime: null,
        remark: null,
        jobStatus: null,
        dataRealm: null,
        parkCode: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.checkGoodsId)
      this.single = selection.length!==1
      this.multiple = !selection.length
      this.selectList = selection;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加盘库单据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const checkGoodsId = row.checkGoodsId || this.ids
      getReceipt(checkGoodsId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改盘库单据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.checkGoodsId != null) {
            updateReceipt(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReceipt(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const checkGoodsIds = row.checkGoodsId || this.ids;
      this.$modal.confirm('是否确认删除盘库单据编号为"' + checkGoodsIds + '"的数据项？').then(function() {
        return delReceipt(checkGoodsIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('inventory/receipt/export', {
        ...this.queryParams
      }, `receipt_${new Date().getTime()}.xlsx`)
    },
    computeTableHeight(){
      if( this.showSearch==true ){
        this.tableHeight = window.innerHeight - this.queryHeight - 28 - 28 - 160;
      } else {
        this.tableHeight = window.innerHeight - 28 - 28 - 160;
      }
    },
    handleInventoryRevoke(row) {
      console.log("handleInventoryRevoke");
      const data = {
        checkGoodsId: this.selectList[0].checkGoodsId,
        jobStatus: this.selectList[0].jobStatus,
      };
      if (this.selectList[0].numberOfPiece > 0) {
        this.$modal.confirm('是否撤销"' + this.selectList[0].checkGoodsNumber + '"完成？').then(function () {
          return revokeCheckGoods(data);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("确认成功");
        }).catch(() => { });
      } else {
        this.$modal.msgError("请先添加货物");
      }
    },

  }
};
</script>
